﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpTrainer.Util;
using Zeus.Thunder.Core;

namespace SharpTrainer.Other
{
    class MemoryLinq : ITestCase
    {
        public void Run()
        {
            var products = new List<Product> {
                new Product { Name = "Chai", Price = 18.00m },
                new Product { Name = "Chang", Price = 19.00m },
                new Product { Name = "Aniseed Syrup", Price = 10.00m },
                new Product { Name = "Chef Anton’s Cajun Seasoning", Price = 22.00m },
                new Product { Name = "Chef Anton’s Gumbo Mix", Price = 21.35m },
                new Product { Name = "Grandma’s Boysenberry Spread", Price = 25.00m },
                new Product { Name = "Uncle Bob’s Organic Dried Pears", Price = 30.00m },
                new Product { Name = "Northwoods Cranberry Sauce", Price = 40.00m },
                new Product { Name = "Mishi Kobe Niku", Price = 97.00m },
                new Product { Name = "Ikura", Price = 31.00m }
            };

            var cheap = from product in products
                        where product.Price < 25
                        select product;
            Console.WriteLine("Cheap:");
            Display(cheap);

            var expensive = (from product in products
                            where product.Price > 20
                            select product).Take(2);  //select the first two
            Console.WriteLine("Expensive:");
            Display(expensive);

            Console.WriteLine("Price Group:");
            var priceGroups = from product in products
            group product by 10m * (int)(product.Price / 10);
            foreach (var priceGroup in priceGroups) 
            {
                Console.WriteLine("From {0} to {1}", priceGroup.Key, priceGroup.Key + 9.99m);
                foreach (var product in priceGroup)
                    Console.WriteLine("{0} costs ${1}", product.Name, product.Price);
                Console.WriteLine("-----------------------------------------------------");
            }
        }

        private void Display(IEnumerable<Product> products)
        {
            GenericUtil<Product>.PrintEnumerable(products);
        }
    }
}
